// 部门管理
<template>
  <div class="app-container">
  <el-card class="box-card" shadow="never" style="border:0px;">
      <div slot="header" style="color:#303133;font-size:14px;font-weight:bold;">
        <span>员工管理</span>
      </div>
    <div class="filter-container">
      <!-- <div class="right">
        <el-button type="primary" size="small" icon="el-icon-plus" round @click="openDeptUsers()">选择员工</el-button>
      </div> -->
    </div>
    <vxe-toolbar ref="xToolbar">
         <template #buttons>
           <el-button type="text" icon="el-icon-plus" @click="openDeptUsers()">选择员工</el-button>
        </template>
    </vxe-toolbar>
    <vxe-table
      v-loading="loading"
      :data="queryData"
      height="530"
      border="none"
      highlight-hover-row
      show-header-overflow
      show-overflow
      align="center"
      :header-cell-style="{ background: '#F7F7F8', color: '#606266' }"
    >
      <vxe-table-column type="index" width="50" title="序号"></vxe-table-column>
      <vxe-table-column title="姓名" field="displayName" />
      <vxe-table-column title="手机号" field="phone" />
      <vxe-table-column title="邮箱" field="email" />
      <vxe-table-column title="进件数量" field="projectNum" />
      <vxe-table-column title="角色" field="roleName" />
      <vxe-table-column title="账号状态" field="accountStatusName">
        <template slot-scope="scope">
           <span>
              <span>
                <i
                  class="smallCircle"
                  :class="[
                    scope.row.accountStatus =='1' ? 'text_active' : 'text_noActive'
                  ]"
                />{{ scope.row.accountStatus =='1' ? '启用' : '禁用' }}
              </span>
            </span>
        </template>
      </vxe-table-column>
      <vxe-table-column title="创建时间" field="createTime" />
      <vxe-table-column title="操作" fixed="right" width="200">
        <template v-slot="{ row }">
          <el-button type="text"  @click="removeUser(row.id)">移出</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-button type="text"  @click="enableUser(row)" v-if="row.accountStatus === '0'">启用</el-button>
          <el-button type="text"  @click="disableUser(row)" v-if="row.accountStatus === '1'">禁用</el-button>
        </template>
      </vxe-table-column>
    </vxe-table>

    <!--  选择员工 -->
    <vxe-modal
      @close="closeModal"
      v-model="getDeptUsers.show"
      width="700px"
      height="250px"
      :title="'选择员工'"
    >
     <el-form>
        <el-form-item label="员工" label-width="150px" prop="userIds">
          <el-select v-model="getDeptUsersForm.userIds" placeholder="请选择员工" style="width: 400px" multiple>
            <el-option v-for="(item, index) in deptUser" :key="index" :label="item.displayName + '：' + item.phone" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label-width="150px">
          <el-button type="primary" @click="insertDeptUser('getDeptUsersForm')" status="primary">确定</el-button>
          <el-button @click="fresh('getDeptUsersForm')" status="danger">取消</el-button>
        </el-form-item>
      </el-form>
    </vxe-modal>

    <!-- 编辑员工 -->
     <vxe-modal
      @close="closeModal"
      v-model="userUpdate.show"
      width="700px"
      height="350px"
      :title="'编辑'"
    >
     <el-form>
        <el-form-item label="姓名" prop="displayName" label-width="100px">
          <el-input v-model="userUpdateForm.displayName" placeholder="请输入部门名称" clearable style="width: 400px"></el-input>
        </el-form-item>
        <el-form-item label-width="300px">
          <el-button type="primary" @click="insertDeptUser('getDeptUsersForm')" status="primary">确定</el-button>
          <el-button @click="closeUserUpdate('getDeptUsersForm')" status="danger">取消</el-button>
        </el-form-item>
      </el-form>
    </vxe-modal>
  </el-card>
    <vxe-pager
      class="pager"
      @page-change="changePage"
      :current-page.sync="listQuery.pageNo"
      :page-size.sync="listQuery.pageSize"
      :total="total"
      :page-sizes="[10, 30, 50,100]"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
    >
    </vxe-pager>


  </div>
</template>

<script>
import { findUserByDeptId,disableOrEnableAccount,findNotUserByDeptId,insertDeptUser,removeUser } from '@/api/dept';
export default {
  name: 'dept',
  data: function () {
    return {
      active: 0,
      queryData: [],
      total: 0,
      listQuery: {
        pageNo: 1,
        pageSize: 10,
        deptId: undefined,
      },
      loading: true,
      deptId: undefined,
      getDeptUsersForm: {
        deptId: undefined,
        deptName: undefined,
        deptManagerId: undefined,
        userIds: undefined,
      },
      getDeptUsers: {
        show: false,
        edit: false,
        originRow: {},
      },
      userUpdate: {
        show: false,
        edit: false,
        originRow: {},
      },
      userUpdateForm: {
        id: undefined,
        deptName: undefined,
        deptManagerId: undefined,
        userIds: undefined,
      },
      deptUser: [],
    };
  },
  mounted: function () {
    this.deptId = this.$route.query.deptId;
    this.findUserByDeptId(this.deptId);
  },


  methods: {
    fresh(getDeptUsersForm) {
      this.getDeptUsers.show = false;
      this.getDeptUsers.edit = true;
      this.findUserByDeptId(this.deptId);
      // this.$refs[getDeptUsersForm].resetFields();
      this.restForm();
    },

    closeUserUpdate() {
      this.userUpdate.show = false;
      this.userUpdate.edit = true;
      this.findUserByDeptId(this.deptId);
      // this.$refs[getDeptUsersForm].resetFields();
      this.restForm();
    },

    // 打开窗口
    openDeptUsers() {
      this.getDeptUsers.show = true;
      this.getDeptUsers.edit = false;
      this.findNotUserByDeptId();
      this.restForm();
    },
    closeModal() {
      this.modalUpdate.row = {};
      this.modalUpdate.originRow = {};
      this.modalUpdate.show = false;
      this.modalUpdate.edit = false;
    },
    // 移出
    removeUser(userId) {
      this.$confirm('确定要执行移出操作吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          removeUser(userId,this.deptId).then((data) => {
            this.fresh();
            this.$notify.success({title: '成功',message: '移出成功',});
          });
        })
        .catch(() => {this.$message({type: 'info',message: '已取消',});});
    },


    // 新增
    insertDeptUser() {
      this.getDeptUsersForm.deptId=this.deptId;
       insertDeptUser(this.getDeptUsersForm).then((data) => {
        this.fresh();
      });
    },

    changePage() {
      this.deptId = this.$route.query.deptId;
      this.findUserByDeptId(this.deptId);
    },

    // 根据部门id查询还没有选过的员工
    findNotUserByDeptId() {
      findNotUserByDeptId(this.deptId).then((data) => {
        this.deptUser = data.data;
      });
    },

    // 禁用或启用账号
    disableOrEnableAccount(param) {
      disableOrEnableAccount(param).then((data) => {
        this.fresh();
      });
    },

    //  启用账号
    enableUser(row) {
      row.accountStatus = "1";
      this.disableOrEnableAccount(row);
    },
    //  禁用账号
    disableUser(row) {
      row.accountStatus = "0";
      this.disableOrEnableAccount(row);
    },


    findUserByDeptId(deptId) {
      this.loading = true;
      this.listQuery.deptId=deptId;
      findUserByDeptId(this.listQuery).then((data) => {
        this.queryData = data.data;
        this.total = data.total;
        console.log('响应数据:%o', data);
        this.loading = false;
      });
    },


    restForm() {
      this.listQuery = {
        pageNo: 1,
        pageSize: 10,
        name: undefined,
        mobile: undefined,
      };
      this.getDeptUsersForm = {
        userIds: undefined,
        deptId: undefined,
      };
      this.findUserByDeptId(this.deptId);
    },

  },
};
</script>

<style lang="css" scoped>
.filter-container {
  display: flex;
  justify-content: space-between;
}

.pager{
  position: fixed;
  bottom: 0;
  right: 40px;
  background-color: #FAFAFB;
  z-index: 2;
  height: 40px;
  }

.right >>> .el-button--primary,.el-button--default {
  padding: 10px 20px;
}

.vxe-table--render-default >>> .el-button--text{
  color: #2b7bfb;
}
.app-container >>> .vxe-table--render-default,.vxe-pager {
  font-family: PingFangSC, Arial, Helvetica, sans-serif;
} 

.vxe-table--render-default >>> .el-button--text{
  color: #2b7bfb;
}
.app-container >>> .vxe-table--render-default {
  font-family: PingFangSC, Arial, Helvetica, sans-serif;
} 

.left {
  flex: 8;
  display: flex;
  flex-flow: row wrap;
  margin-right: 20px;
}
.left >>> .el-form {
  width: 100%;
}
.left >>> .el-form .el-form-item {
  display: flex;
  flex-flow: row nowrap;
  width: 30%;
  margin-right: 3%;
  float: left;
  margin-bottom: 14px;
}

.left >>> .el-form .el-form-item .el-form-item__label {
  font-weight: 500;
  color: #606266;
  text-align: left;
  /* flex: 1; */
}

.left >>> .el-form .el-form-item .el-form-item__content {
  flex: 1;
  /*超出部分隐藏*/
  text-overflow: ellipsis;
  /* 超出部分显示省略号 */
  white-space: nowrap;
  /*规定段落中的文本不进行换行 */
  width: 100%;
}
.date_item {
  width: 100%;
}
.right {
  flex: 2;
  display: flex;
  flex-flow: row nowrap;
  align-items: flex-end;
  margin-right: 30px;
  margin-bottom: 30px;
}
</style>
